//
//  HomeViewController.swift
//  SinaWeibo
//
//  Created by paycloud110 on 16/5/30.
//  Copyright © 2016年 paycloud110. All rights reserved.
//

import UIKit

class HomeViewController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !loginIn {
            vistorView?.setupVistorView(nil, titleLabel: nil)
            vistorView?.setupAnimation()
            return
        }
        let leftButton = UIButton()
        leftButton.setImage(UIImage(named: "navigationbar_friendattention"), forState: .Normal)
        leftButton.setImage(UIImage(named: "navigationbar_friendattention_highlighted"), forState: .Highlighted)
        leftButton.sizeToFit()
        leftButton.addTarget(self, action: Selector("myselfButtonClick"), forControlEvents: .TouchUpInside)
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftButton)

        navigationItem.rightBarButtonItem = UIBarButtonItem.init(imageName: "navigationbar_pop", target: self, action: Selector("QRCoderButtonClick"))

        titleButton.addTarget(self, action: Selector("titleButtonClick:"), forControlEvents: .TouchUpInside)
        navigationItem.titleView = titleButton
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("titleButtonChange"), name: PopoverViewControllerShowClick, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("titleButtonChange"), name: PopoverViewControllerDismissClick, object: nil)
    }
    @objc private func titleButtonChange() {
    
        JWNLog("ddffffff")
        titleButton.selected = !titleButton.selected
    }
    @objc private func titleButtonClick(titleBUtton: UIButton) {
        
        let storyBD = UIStoryboard(name: "PopoverViewController", bundle: nil)
        let popoverVC = storyBD.instantiateInitialViewController()!
        
        /** 自定义转场动画 保留旧的控制器同时也显示新的控制器 */
        popoverVC.transitioningDelegate = popoverControllerManager
        popoverVC.modalPresentationStyle = .Custom
        presentViewController(popoverVC, animated: true, completion: nil)
    }
    @objc private func myselfButtonClick() {
        
        JWNLog("dd")
    }
    @objc private func QRCoderButtonClick() {
        
        let stroyBoard = UIStoryboard(name: "QRCodeViewController", bundle: nil)
        let QRCoderVC = stroyBoard.instantiateInitialViewController()!
        presentViewController(QRCoderVC, animated: true, completion: nil)
    }
    
    // MARK: - layz
    private lazy var titleButton: TitleButton = {
        
        let button = TitleButton()
        return button
    }()
    private lazy var popoverControllerManager: PopoverControllerAnimationManager = {
        
        let manager = PopoverControllerAnimationManager()
        let deviceWidth = UIScreen.mainScreen().bounds.size.width
        let popWidth = 200
        let x = (deviceWidth - CGFloat(popWidth)) / 2.0
        manager.presentedFrame = CGRect(x: CGFloat(x), y: CGFloat(55), width: CGFloat(popWidth), height: CGFloat(300))
        return manager
    }()
}

